﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "过站分析";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 45, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 45, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 45, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 45, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <form id="form" class="layui-form layui-card-header layuiadmin-card-header-auto" action="/Report/BatchExportViaSiteAnalysis" method="post">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode" @(workshops.IndexOf(item) == 0 ? " selected " : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" name="lineId" v-html="lang.Field.AssemblyLine">流水线：</label>
                        <div class="layui-input-inline">
                            <select id="selLines" lay-filter="selLines">
                                <option value="0">全部</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="@ViewBag.OrderNo" @(!string.IsNullOrWhiteSpace(ViewBag.OrderNo) ? " disabled " : "") autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ProduceTime">生产时间：</label>
                        <div class="layui-input-inline" style="width:440px;">
                            <input type="text" id="txtRangeTime" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-top:-6px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button" style="margin-top:-6px;margin-left:0;">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" style="margin-top:-6px;" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnExport" class="layui-btn layui-btn-normal layuiadmin-btn-list" style="margin-top:-6px;" target="_blank" href="javascript:void(0)">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>报表下载
                        </a>
                        <button id="btnBatchDownload" class="layui-btn layuiadmin-btn-list" style="margin-top:-6px;" type="button">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>组件批量查询
                        </button>
                        <input type="hidden" value="" name="barCodes" />
                    </div>
                </div>
            </form>
            <div class="layui-card-body" id="chart" style="height:500px;"></div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script src="/res/echarts/echarts.min.js"></script>
    <script src="/res/echarts/macarons.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package,
                searchData: {}
            }
        });
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['全线']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.Sites))
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: '全线',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        show: true,
                        position: 'inside'
                    },
                    data: []
                }
            ]
        };
        var myChart = echarts.init(document.getElementById('chart'), 'macarons');
        myChart.on('click', function (params) {
            var siteType = params.name;
            vm.$data.searchData.siteType = siteType;
            parent.parent.layui.index.openTabsPage("/Report/ViaSiteAnalysisDetail?paras=" + encodeURIComponent(JSON.stringify(vm.$data.searchData)), "过站记录(" + siteType + ")");
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate', 'element'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                setTimeout(function () {
                    laydate.render({
                        elem: '.rangetime'
                        , type: 'datetime'
                        , rigger: 'click'
                        , range: '至'
                        , done: function (value, date, endDate) {
                            queryNos(0);
                            queryNos(1);
                        }
                    });
                }, 100);
                form.on("select(selWorkshops)", function (data) {
                    queryNos(0);
                    queryNos(1);
                    queryLines();
                });
                queryNos(0);
                queryNos(1);
                queryLines();

                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");

                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }
                function queryLines() {
                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/QueryLines",
                        data: {
                            workshopCode: $("#selWorkshops").val().trim()
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value='0'>全部</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i].PkId + "'>" + res.Data[i].LineName + "</option>");
                            }
                            $("#selLines").html(html.join(""));
                            form.render("select");
                        }
                    });
                }

                $("#btnSearch").click(function () {
                    queryViaSiteAnalysis();
                });
                queryViaSiteAnalysis();
                function queryViaSiteAnalysis() {
                    var workShopCode = $("#selWorkshops").val(),
                        lineId = parseInt($("#selLines").val()),
                        orderNo = $("#txtOrderNo").val().trim(),
                        workOrderNo = $("#txtWorkOrderNo").val().trim(),
                        rangeTime = $("#txtRangeTime").val().trim();
                    var startTime = rangeTime == '' ? null : rangeTime.split('至')[0].trim(),
                        endTime = rangeTime == '' ? null : rangeTime.split('至')[1].trim();
                    vm.$data.searchData = {
                        workShopCode: workShopCode,
                        lineId: lineId,
                        orderNo: orderNo,
                        workOrderNo: workOrderNo,
                        startTime: startTime,
                        endTime: endTime
                    };
                    $("#btnExport").attr("href", "/Report/ExportViaSiteAnalysis?workshopCode=" + workShopCode + "&lineId=" + lineId + "&orderNo=" + orderNo + "&workOrderNo=" + workOrderNo + "&startTime=" + startTime + "&endTime=" + endTime);

                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/QueryViaSiteAnalysis",
                        data: vm.$data.searchData,
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var values = new Array;
                            for (var i = 0; i < res.Data.length; i++) {
                                values.push(res.Data[i].Value);
                            }
                            option.series = {
                                name: "",
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    show: true,
                                    position: 'inside'
                                },
                                data: values
                            };
                            myChart.setOption(option);
                        }
                    });
                }
            });

            $("#btnBatchDownload").click(function () {
                layer.prompt({
                    formType: 2,
                    title: '请输入组件条码',
                    maxlength: 100000,
                    area: ['800px', '350px'] //自定义文本域宽高
                }, function (value, index, elem) {
                    var barCodes = value.split('\n');
                    $("input[name=barCodes]").val(barCodes.join(','));
                    $("#form").submit();
                    layer.close(index);
                });
            });
        }
    </script>
}
